package com.yt.sys.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.sys.models.bo.SysPermissionSaveBO;
import com.yt.sys.models.dto.SysRolePermissionDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import com.yt.sys.models.service.ISysPermissionService;
import com.yt.sys.models.bo.SysPermissionBO;
import com.yt.sys.models.dto.SysPermissionDTO;

import java.util.Date;
import java.util.List;

/**
 * 登录信息控制器
 *
 * @author lizicong
 * @Date 2025-10-25 00:30:50
 */

@RestController
@RequestMapping("/api/sysPermission")
@Slf4j
@Api(value = "SysPermissionApiController",tags = "登录信息Api接口")
public class SysPermissionApiController {

    @DubboReference
    private ISysPermissionService sysPermissionService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SysPermissionDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        SysPermissionDTO dto = sysPermissionService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "登录信息列表",notes = "登录信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysPermissionDTO>> queryPageList(SysPermissionBO bo){
        Wrapper<SysPermissionDTO> wrapper = new EntityWrapper<>();
                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<SysPermissionDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = sysPermissionService.selectPage(page, wrapper);

        return new Result<>(page);
    }
    @ApiOperation(value = "根据权限名称模糊查询", notes = "根据权限名称进行模糊查询")
    @PostMapping("/listByName")
    public Result<List<SysPermissionDTO>> listByName(@RequestParam(required = false) String permissionName) {
        EntityWrapper<SysPermissionDTO> wrapper = new EntityWrapper<>();
        if (permissionName != null && !permissionName.trim().isEmpty()) {
            wrapper.like("permission_name", permissionName.trim());
        }
        wrapper.orderBy("id", false);
        List<SysPermissionDTO> list = sysPermissionService.selectList(wrapper);
        return new Result<>(list);
    }

    @ApiOperation(value = "新增权限", notes = "新增权限")
    @PostMapping("/add")
    public Result<String> add(@RequestBody SysPermissionSaveBO saveBO) {
        // 参数校验
        if (saveBO == null) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "权限信息不能为空");
        }

        if (saveBO.getPermissionName() == null || saveBO.getPermissionName().trim().isEmpty()) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "权限名称不能为空");
        }

        if (saveBO.getPermissionCode() == null || saveBO.getPermissionCode().trim().isEmpty()) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "权限编码不能为空");
        }

        if (saveBO.getType() == null) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "权限类型不能为空");
        }

        if (saveBO.getStatus() == null) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "状态不能为空");
        }

        // 创建DTO对象并设置时间字段
        SysPermissionDTO dto = new SysPermissionDTO();
        dto.setPermissionName(saveBO.getPermissionName());
        dto.setPermissionCode(saveBO.getPermissionCode());
        dto.setType(saveBO.getType());
        dto.setUrl(saveBO.getUrl());
        dto.setStatus(saveBO.getStatus());

        // 设置创建时间和更新时间
        Date now = new Date();
        dto.setCreateTime(now);
        dto.setUpdateTime(now);

        // 设置乐观锁版本号
        dto.setRevision(0);

        // 调用服务新增权限
        boolean result = sysPermissionService.insert(dto);
        if (result) {
            return new Result<>(CodeEnum.SUCCESS.getCode(), "权限新增成功");
        } else {
            return new Result<>(CodeEnum.FAIL.getCode(), "权限新增失败");
        }
    }

    @ApiOperation(value = "编辑权限", notes = "编辑权限")
    @PostMapping("/edit")
    public Result<String> edit(@RequestBody SysPermissionSaveBO saveBO) {
        // 参数校验
        if (saveBO == null) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "权限信息不能为空");
        }

        if (saveBO.getId() == null) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "权限ID不能为空");
        }

        if (saveBO.getPermissionName() == null || saveBO.getPermissionName().trim().isEmpty()) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "权限名称不能为空");
        }

        if (saveBO.getPermissionCode() == null || saveBO.getPermissionCode().trim().isEmpty()) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "权限编码不能为空");
        }

        if (saveBO.getType() == null) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "权限类型不能为空");
        }

        if (saveBO.getStatus() == null) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "状态不能为空");
        }

        // 检查权限是否存在
        SysPermissionDTO permission = sysPermissionService.selectById(saveBO.getId());
        if (permission == null) {
            return new Result<>(CodeEnum.FAIL_100002.getCode(), "权限不存在");
        }

        // 更新权限信息
        permission.setPermissionName(saveBO.getPermissionName());
        permission.setPermissionCode(saveBO.getPermissionCode());
        permission.setType(saveBO.getType());
        permission.setStatus(saveBO.getStatus());
        permission.setUrl(saveBO.getUrl());
        permission.setUpdateTime(new Date());
        // 更新乐观锁版本号
        if (permission.getRevision() != null) {
            permission.setRevision(permission.getRevision() + 1);
        } else {
            permission.setRevision(1);
        }

        boolean result = sysPermissionService.updateById(permission);
        if (result) {
            return new Result<>(CodeEnum.SUCCESS.getCode(), "权限编辑成功");
        } else {
            return new Result<>(CodeEnum.FAIL.getCode(), "权限编辑失败");
        }
    }
    @ApiOperation(value = "获取所有权限信息", notes = "获取所有权限信息")
    @PostMapping("/listAll")
    public Result<List<SysPermissionDTO>> listAll() {
        List<SysPermissionDTO> list = sysPermissionService.selectList(null);
        return new Result<>(list);
    }
}
